package com.laoye.big_event.Service.Impl;

import com.laoye.big_event.Mapper.UserMapper;
import com.laoye.big_event.Pojo.User;
import com.laoye.big_event.Service.UserService;
import com.laoye.big_event.Utils.Md5Util;
import com.laoye.big_event.Utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User SelectByUsername(String username) {
        return userMapper.SelectByUsername(username);
    }

    @Override
    public void RegisterUser(String username, String password) {
        if (username == null || password == null) {
            throw new IllegalArgumentException("用户名和密码不能为空");
        }
        //加密密码
        String md5String = Md5Util.getMD5String(password);
        //添加密码
        userMapper.RegisterUser(username,md5String);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
//        将id传给userMapper
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd,int id) {
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }
}
